/* eslint-disable */
/**
 * Created by GYL on 2018/8/24
 */
//事件配置管理
import React, { Component } from 'react';
import Filtrate from '../../components/common/Filtrate';
import Container from "../../components/common/Container";
import MyTable from "../../components/common/MyTable";
import PublicService from "../../services/PublicService";
import request from '../../utils/request';
import MyPagination from "../../components/common/MyPagination";
import { message, Spin } from 'antd';
import GtteWrap from './sub/GtteWrap';
import PublicTemplate from '../../services/PublicTemplate';
import moment from 'moment';
import Edit_Use from './sub/Edit_Use';
import styles from './ConfigManage.less'
class ConfigManage extends Component {

  state = {
    warpFlag: true,
    params: {},
    pageSize: 10,
    pageIndex: 1,
    total: 0,
    delConfirm: false,
    addModal: false,
    selectedRowKeys: [],
    dataSate: [],
    addWrap: false,
    editWrap: false,
    EditArr: [],
    AddArr: [],
    GtteWrapId: '',
    title: '',
    MyModal1VisibleSmall: false,
    Edit_UseData: {},
    spinning: false,
    modalBtnLoading: false,
    xmmc: [],
    ywjl: [],
    xmfzr: [],
    qyfzr: []
  }
  componentDidMount() {
    this.onSubmit(1, 10)

    this.dropDownBoxTwo()
  }

  dropDownBoxTwo = () => {
    request({
      url: '/pm-web/projectmanage/findData/dropDownBoxTwo',
      method: 'GET',
      params: {
        userId: PublicService.getCookie('userId')
      }
    }).then(data => {

      if (data.rc === 0) {
        this.setState({
          xmmc: data.ret.xmmc,
          ywjl: data.ret.ywjl,
          xmfzr: data.ret.xmfzr,
          qyfzr: data.ret.qyfzr,
        })
      }
    })
  }
  //查询
  onSubmit = (page, size) => {
    const params = this.f1.getForm().getFieldsValue();
    params.page = page;
    params.rows = size;
    params.userId = PublicService.getCookie('userId'),
      this.setState({
        params,
        spinning: true,
        pageIndex: page,
        pageSize: size
      })

    request({
      url: '/pm-web/projectmanage/projectinfo/getAllProjectInfo',
      method: 'POST',
      data: params
    }).then(data => {
      // console.log(data)

      if (data.rc === 0) {



        this.setState({
          dataSate: data.ret.items,
          spinning: false,
          total: data.ret.rowCount,
          selectedRowKeys: [],
        })
      }
    })
  }

  //重置
  ResetForm = () => {
    console.log('重置')
    this.setState({
      pageIndex: 1,
      pageSize: 10
    }, () => {
      this.onSubmit(1, 10)
    })
  }

  //新增
  addBtnText = () => {
    // console.log('新增')
    this.setState({
      addWrap: true
    })
  }

  // 取消保存
  ModalCancel = (type) => {
    let t = this;
    t.setState({
      [type]: false,
      modalBtnLoading: false
    })
  };

  //多选
  onSelectChange = (selectedRowKeys, row) => {
    console.log(selectedRowKeys, row)
    if (selectedRowKeys.length < 1) {
      this.setState({ selectedRowKeys, row })
    } else {
      this.setState({
        selectedRowKeys: [selectedRowKeys[selectedRowKeys.length - 1]],
        row: [row[row.length - 1]]
      })
    }

  };

  //编辑按钮
  editBtn = () => {
    const { dataSate, selectedRowKeys } = this.state;
    if (selectedRowKeys.length !== 1) {
      message.warning('只能对单一事件编辑!')
      return
    }

    if (dataSate[selectedRowKeys[0]].status != 2) {
      message.warning("项目未搭建")
      return
    }
    // console.log(dataSate[selectedRowKeys[0]].configurationStatus)

    if (dataSate[selectedRowKeys[0]].configurationStatus == '3' || dataSate[selectedRowKeys[0]].configurationStatus == '5') {
      message.warning("项目已配置")
      return
    }
    request({
      url: '/pm-web/projectmanage/eventinfo/judgeConfigurationAuthority',//查询是否具有权限
      method: 'POST',
      data: { userId: PublicService.getCookie('userId'), buttonName: '事件配置', projectId: dataSate[selectedRowKeys[0]].projectId }
    }).then(data => {
      if (data.rc === 0) {
        if (data.ret.type) {

          if (dataSate[selectedRowKeys[0]].paymentMethod) {
            this.setState({
              GtteWrap: true,
              GtteWrapId: dataSate[selectedRowKeys[0]].projectId,
              title: dataSate[selectedRowKeys[0]].projectName,
              warpFlag: true
            })
          } else {
            this.repairBtn();
          }

        } else {
          message.warning("用户权限不足!")
        }
      }
    })





  }
  //查看按钮
  examineBtn = () => {
    const {selectedRowKeys,dataSate} = this.state;
    if (selectedRowKeys.length !== 1) {
      message.warning('只能对单一事件查看!')
      return
    }

    if (dataSate[selectedRowKeys[0]].status != 2) {
      message.warning("项目未搭建")
      return
    }
    request({
      url: '/pm-web/projectmanage/projectinfo/getProjectInfo',
      method: 'POST',
      data: { projectId: dataSate[selectedRowKeys[0]].projectId }
    }).then(data => {
      if (data.rc === 0) {
        this.setState({
          Edit_UseData: data.ret
        }, () => {
          this.setState({
            GtteWrap: true,
            GtteWrapId: dataSate[selectedRowKeys[0]].projectId,
            title: dataSate[selectedRowKeys[0]].projectName,
            warpFlag: false
          })
        })
      }

    })
  }

  goBack = () => {
    this.setState({
      GtteWrap: false
    })
    setTimeout(() => {
      this.onSubmit(1, 10)
    }, 200)
  }

  //子事件保存
  onModalSaveSmall = (v) => {
    const {dataSate,selectedRowKeys} = this.state;
    this.setState({
      modalBtnLoading: true
    })
    request({
      url: '/pm-web/projectmanage/projectinfo/updateProjectInfoTwo',
      method: 'POST',
      data: { ...v, projectId: dataSate[selectedRowKeys[0]].projectId }
    }).then(data => {
      if (data.rc === 0) {
        this.setState({
          MyModal1VisibleSmall: false
        })
        this.setState({
          modalBtnLoading: false
        })
        this.onSubmit(1,10);
      }

    })


  }


  //导出
  exportBtn = (val) => {
    let t = this;
    let isAll = val === 'all' ? true : false;
    window.location.href = '/pm-web/projectmanage/projectinfo/exportProjectMessage?' + PublicService.paramSerializer({
      ...t.state.params,
      isAll
    })
  };

  //编辑付款方式
  repairBtn = () => {
    const { dataSate, selectedRowKeys } = this.state;
    if (selectedRowKeys.length !== 1) {
      message.warning('只能对单一事件编辑!')
      return
    }
    if (dataSate[selectedRowKeys[0]].status != 2) {
      message.warning("项目未搭建")
      return
    }

    request({
      url: '/pm-web/projectmanage/eventinfo/judgeConfigurationAuthority',//查询是否具有权限
      method: 'POST',
      data: { userId: PublicService.getCookie('userId'), buttonName: '事件配置', projectId: dataSate[selectedRowKeys[0]].projectId }
    }).then(data => {
      if (data.rc === 0) {
        if (data.ret.type) {

          request({
            url: '/pm-web/projectmanage/projectinfo/getProjectInfo',
            method: 'POST',
            data: { projectId: dataSate[selectedRowKeys[0]].projectId }
          }).then(data => {
            if (data.rc === 0) {
              this.setState({
                Edit_UseData: data.ret
              }, () => {
                this.setState({
                  MyModal1VisibleSmall: true,

                })
              })
            }

          })

        } else {
          message.warning("用户权限不足!")
        }
      }else{
        message.warning(data.err)
      }
    })

  }

  render() {
    let t = this;
    let width = document.body.clientWidth;
    // console.log(width)
    const height = document.body.clientHeight;
    let containerHeight = '83vh';
    let autoCompleteWidth = 300
    let tableHeight = 280;
    if (width < 1920) {
      tableHeight = 280;
      containerHeight = '76vh';
      autoCompleteWidth = 500
    }
    const { qyfzr, xmmc, pageSize, pageIndex, selectedRowKeys, total, dataSate, delConfirm, modalBtnLoading, GtteWrapId, title } = this.state
    let rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectChange.bind(this),
    };

    const items = [
      {
        type: 'autoComplete',
        label: '项目名称',
        placeholder: '请输入 ..',
        paramName: 'projectName',
        options: xmmc,
        width:autoCompleteWidth
      },
      {
        type: 'autoComplete',
        label: '区域负责人',
        placeholder: '请输入 ..',
        paramName: 'areaManagerName',
        options: qyfzr
      },
      {
        type: 'select',
        label: '配置状态',
        paramName: 'configurationStatus',
        options: [
          { text: '全部', value: '' },
          { text: '尚未创建', value: '1' },
          { text: '已存草稿', value: '2' },
          { text: '审批中', value: '3' },
          { text: '审批拒绝', value: '4' },
          { text: '已配置', value: '5' },
          { text: '审批撤回', value: '6' },
        ],
      },
    ];


    const columns = [
      { title: '序号', dataIndex: 'num', width: 50, fixed: 'left' },
      { title: '项目名称', dataIndex: 'projectName', key: 'projectName', width: 200, fixed: 'left' },
      { title: '项目编号', dataIndex: 'projectNumber', key: 'projectNumber', width: 150 },
      { title: '项目负责人', dataIndex: 'projectManagerName', key: 'projectManagerName', width: 100, align: 'center' },
      { title: '区域负责人', dataIndex: 'areaManagerName', key: 'areaManagerName', width: 100, align: 'center' },
      { title: '负责人联系方式', dataIndex: 'projectContactWay', key: 'projectContactWay', width: 150, align: 'center' },
      { title: '项目搭建状态', dataIndex: 'djType', key: 'djType', width: 100 },
      {
        title: '事件配置状态', dataIndex: 'pzType', key: 'pzType', width: 150, align: 'center',
        // render: (text, record, index) => <span>
        //   <span>{record.configurationStatus == 1?'尚未创建':record.configurationStatus == 2?'已存草稿':record.configurationStatus == 3?'审批中':record.configurationStatus == 4?'审批拒绝':record.configurationStatus == 5?'已配置':'审批撤回'}</span>
        // </span>
      },
      {
        title: '付款方式', dataIndex: 'paymentMethod', key: 'paymentMethod', width: 150, align: 'center',
        render: (text, record, index) => <span>
          {PublicTemplate.stringToEllipsis(text, 30)}
        </span>
      },
      { title: '中标日期', dataIndex: 'winningBidDate', key: 'winningBidDate', width: 150, align: 'center' },
      { title: '中标金额(万元)', dataIndex: 'winningBidSum', key: 'winningBidSum', width: 150 },
      { title: '计划开始时间', dataIndex: 'planStartTime', key: 'planStartTime', width: 150 },
      { title: '计划结束时间', dataIndex: 'planEndTime', key: 'planEndTime', width: 150 },
      { title: '计划工期', dataIndex: 'workTime', key: 'workTime', width: 150 },
      { title: '主要工艺', dataIndex: 'theMainProcess', key: 'theMainProcess', width: 150 },
      { title: '项目地点', dataIndex: 'projectSite', key: 'projectSite', width: 150 },
      { title: '单位分类', dataIndex: 'unitClassification', key: 'unitClassification', width: 150 },
      { title: '单位名称', dataIndex: 'unitName', key: 'unitName', width: 150 },
      { title: '业务经理', dataIndex: 'serviceManagerName', key: 'serviceManagerName', width: 150 },
      { title: '业务经理电话', dataIndex: 'serviceContactWay', key: 'serviceC31ontactWay', width: 150 },
      { title: '计划产值(元)', dataIndex: 'planProduction', key: 'planProduction', width: 150 },
      { title: '累计完成产值(元)', dataIndex: 'accomplishProduction', key: 'accomplishProduction', width: 150 },
      { title: '产值完成比例', dataIndex: 'completeProportion', key: 'completeProportion', width: 150 },
      { title: '项目预算执行率', dataIndex: 'implementationRate', key: 'implementationRate', width: 150 },
      { title: '项目已发生成本(元)', dataIndex: 'incurredCost', key: 'incurredCost', width: 150 },
      { title: '累计应收款额(元)', dataIndex: 'collectedAmount', key: 'collectedAmount', width: 150 },
      { title: '本月回款(元)', dataIndex: 'monthMoney', key: 'monthMoney', width: 150 },
      { title: '本年回款(元)', dataIndex: 'yearMoney', key: 'yearMoney', width: 150 },
      { title: '累计回款(元)', dataIndex: 'grandTotalMoney', key: 'grandTotalMoney', width: 150 },
    ];
    return (

      <div style={{ position: 'relative', boxSize: 'border-box', padding: 5, paddingTop: 0 }} className={styles.Warp_box}>
        {
          this.state.GtteWrap ? <GtteWrap
            id={GtteWrapId}
            title={title}
            goBack={this.goBack}
            warpFlag={this.state.warpFlag}
          /> :
            <div style={{ flex: 1 }}>
              {
                this.state.MyModal1VisibleSmall && <Edit_Use
                  ModalCancel={this.ModalCancel.bind(this, 'MyModal1VisibleSmall')}
                  Edit_UseData={this.state.Edit_UseData}
                  onModalSave={this.onModalSaveSmall}
                  warpFlag={this.state.warpFlag}
                  modalBtnLoading={modalBtnLoading}
                />
              }
              <Spin spinning={this.state.spinning} size='large'>
                <Filtrate
                  items={items}
                  ref={ref => { this.f1 = ref }}
                  submit={this.onSubmit.bind(this, 1, 10)}
                  ResetForm={this.ResetForm}
                />
                <Container
                  style={{ height: containerHeight, position: "relative", overflow: 'auto' }}
                  // header={true}
                  editBtnShow={true}
                  editBtn={this.editBtn}
                  examineBtn={this.examineBtn}
                  examineBtnShow={true}
                  exportBtnShow={true}
                  repairBtnShow={true}
                  repairBtn={this.repairBtn}
                  exportBtn={t.exportBtn.bind(t)}>


                  <MyTable
                    rowSelection={rowSelection}
                    columns={columns}
                    scroll={{ y: height - tableHeight, x: 4000 }}
                    dataSource={PublicService.transformArrayData(dataSate, true, true, pageIndex, pageSize)}
                    pagination={false}
                  />
                  <MyPagination
                    showPage
                    pageSize={pageSize}
                    current={this.state.pageIndex}
                    total={total}
                    showSizeChanger
                    showQuickJumper
                    onChange={this.onSubmit}
                    onShowSizeChange={this.onSubmit}
                  />




                </Container>
              </Spin>
            </div>
        }


      </div>
    );


  }
}
export default ConfigManage;

